package com.bailan.foreat.config;

import com.bailan.foreat.interceptors.JwtInterceptor;
import com.bailan.foreat.interceptors.PermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    JwtInterceptor jwtInterceptor;

    @Autowired
    PermissionInterceptor permissionInterceptor;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addViewController("/").setViewName("homeh");
        registry.addViewController("/homeh.html").setViewName("homeh");
        registry.addViewController("/userapp.html").setViewName("userapp");
        registry.addViewController("/userlogin.html").setViewName("userlogin");
        registry.addViewController("/userregister.html").setViewName("userregister");
        registry.addViewController("/usercart.html").setViewName("usercart");
        registry.addViewController("/usercartbyel.html").setViewName("usercartbyel");
        registry.addViewController("/userorder.html").setViewName("userorder");
        registry.addViewController("/usercenter.html").setViewName("usercenter");
        registry.addViewController("/usersetting.html").setViewName("usersetting");
        registry.addViewController("/useraddress.html").setViewName("useraddress");
        registry.addViewController("/addresssetting.html").setViewName("addresssetting");
        registry.addViewController("/whatpoints.html").setViewName("whatpoints");
        registry.addViewController("/work1.html").setViewName("work1");
        registry.addViewController("/work2.html").setViewName("work2");
        registry.addViewController("/storemenu.html").setViewName("storemenu");
        registry.addViewController("/userorderpreview.html").setViewName("userorderpreview");
        registry.addViewController("/userorderpreviewbyel.html").setViewName("userorderpreviewbyel");
        registry.addViewController("/productmain.html").setViewName("productmain");
        registry.addViewController("/freshFrame").setViewName("freshFrame");
        registry.addViewController("/reLogFrame").setViewName("reLogFrame");
        registry.addViewController("/productdetail").setViewName("productdetail");


    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册interceptor
        registry.addInterceptor(jwtInterceptor).addPathPatterns("/**").excludePathPatterns("/**/*fresh*/**");

        List<String> exclude = new ArrayList<>();
        exclude.add("/");
        exclude.add("/static/**");
        exclude.add("/**/*home*/**");
        exclude.add("/**/*app*/**");
        exclude.add("/**/select");
        exclude.add("/**/*all*/**");
        exclude.add("/store/**");
        exclude.add("/user/**");
        exclude.add("/**/*login*/**");
        exclude.add("/**/*reg*/**");
        exclude.add("/**/*fresh*/**");
        exclude.add("/**/*product*/**");
        exclude.add("/**/*work*/**");
        exclude.add("/common/tologin");
        registry.addInterceptor(permissionInterceptor).addPathPatterns("/**").excludePathPatterns(exclude);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //添加资源处理器路径 即每次访问静态资源都得添加"/static/",例如localhost:8080/static/j1.jpg
        //若registry.addResourceHandler("/s/**") 则必须访问localhost:8080/s/j1.jpg
        registry.addResourceHandler("/static/**")
                //添加了资源处理器路径后对应的映射资源路径
                .addResourceLocations("classpath:/static/");
    }
}
